local zhubei = fk.CreateSkill{
    name = "ym1__zhubei",
}
local U = require "packages/utility/utility"

Fk:loadTranslationTable{
    ["ym1__zhubei"] = "筑北",
    [":ym1__zhubei"] = "出牌阶段结束时，你可以依次使用或弃置四张牌，然后选择一项：本轮手牌上限+3，摸三张牌；本轮体力上限+2，回复2点体力；本轮至多受到1点伤害，分配1点伤害。",
    ["#ym1__zhubei-invoke"] = "是否发动 筑北，依次使用或弃置四张牌",
    ["#ym1__zhubei-use"] = "筑北：使用四张牌（%arg/4）",
    ["#ym1__zhubei-discard"] = "筑北：弃置至多%arg张牌（%arg2/4）",
    ["#ym1__zhubei-choose"] = "筑北：选择一项",
    ["#ym1__zhubei1"] = "本轮手牌上限+3，摸三张牌",
    ["#ym1__zhubei2"] = "本轮体力上限+2，回复2点体力",
    ["#ym1__zhubei3"] = "本轮至多受到1点伤害，分配1点伤害",
    ["#ym1__zhubei-damage"] = "筑北：分配1点伤害",
    ["@ym1__zhubei_hp-round"] = "体力上限+",
    ["@ym1__zhubei_card-round"] = "手牌上限+",
    ["@ym1__zhubei_damage-round"] = "受伤上限=",

    ["$ym1__zhubei1"] = "北地雄关如铁，胡马难度阴山。",
    ["$ym1__zhubei2"] = "坚城在此，胡马安敢南窥！",
}
zhubei:addEffect(fk.EventPhaseEnd,{
    anim_type = "defensive",
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(zhubei.name) and player.phase == Player.Play
    end,
    on_cost = function (self, event, target, player, data)
        if player.room:askToSkillInvoke(player,{
            skill_name = zhubei.name,
            prompt = "#ym1__zhubei-invoke",
        }) then
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local i = 0
        while i < 4 do
            local use = room:askToUseRealCard(player, {
                pattern = player:getCardIds("h"),
                skill_name = zhubei.name,
                prompt = "#ym1__zhubei-use:::"..i,
                extra_data = {
                    bypass_times = true,
                    extraUse = true,
                },
                cancelable = true,
                skip = true,
            })
            if use then
                room:useCard(use)
                i = i + 1
            else
                local ids = room:askToDiscard(player,{
                    min_num = 1,
                    max_num = 4-i,
                    include_equip = true,
                    skill_name = zhubei.name,
                    cancelable = false,
                    pattern = ".",
                    prompt = "#ym1__zhubei-discard:::"..U.ConvertNumber(4-i)..":"..i,
                    skip = false,
                })
                if #ids > 0 then
                    i = i + #ids
                else
                    break
                end
            end
        end
        if i >= 4 then
            local choice = room:askToChoice(player,{
                choices = {"#ym1__zhubei1","#ym1__zhubei2","#ym1__zhubei3"},
                skill_name = zhubei.name,
                prompt = "#ym1__zhubei-choose",
            })
            if choice == "#ym1__zhubei1" then
                room:addPlayerMark(player,"@ym1__zhubei_card-round",3)
                room:drawCards(player,3,zhubei.name)
            elseif choice == "#ym1__zhubei2" then
                room:addPlayerMark(player,"@ym1__zhubei_hp-round",2)
                room:changeMaxHp(player,2)
                room:recover({
                    who = player,
                    num = 2,
                    recoverBy = player,
                    skillName = zhubei.name
                })
            else
                room:setPlayerMark(player,"@ym1__zhubei_damage-round",1)
                local tos = room:askToChoosePlayers(player,{
                    targets = room.alive_players,
                    max_num = 1,
                    min_num = 1,
                    prompt = "#ym1__zhubei-damage",
                    skill_name = zhubei.name,
                    cancelable = false,
                })
                if #tos > 0 then
                    room:damage { from = player, to = tos[1], damage = 1, skillName = zhubei.name }
                end
            end
        end
    end,
})
zhubei:addEffect(fk.RoundEnd,{
    can_refresh = function (self, event, target, player, data)
        return player:getMark("@ym1__zhubei_hp-round") > 0
    end,
    on_refresh = function (self, event, target, player, data)
        player.room:changeMaxHp(player,-player:getMark("@ym1__zhubei_hp-round"))
    end,
})
zhubei:addEffect(fk.DamageInflicted,{
    can_refresh = function (self, event, target, player, data)
        local num = 0
        player.room.logic:getActualDamageEvents(1, function(e)
            if e.data.to == player then
                num = num + e.data.damage
            end
            return false
        end, Player.HistoryRound)
        return (num >= 1 or data.damage > 1 ) and player:getMark("@ym1__zhubei_damage-round") > 0 and target == player
    end,
    on_refresh = function (self, event, target, player, data)
        local num = 0
        player.room.logic:getActualDamageEvents(1, function(e)
            if e.data.to == player then
                num = num + e.data.damage
            end
            return false
        end, Player.HistoryRound)
        if num >= 1 then
            data.damage = 0
        else
            data.damage = 1
        end
    end,
})
zhubei:addEffect("maxcards",{
    correct_func = function (self, player)
        return player:getMark("@ym1__zhubei_card-round")
    end,
})
return zhubei